<?php

namespace Our;

class Paginator
{
    public $page;
    public $totalItem;
    public $pageSize;
    public $totalPage;
    public $baseUrl = '';
    public $pageRange = 2;
    public $ext = '.htm';

    public function __construct($page, $totalItem, $pageSize, $options = array())
    {
        if (!($pageSize > 0)) {
            return false;
        }
        $this->setTotalItem($totalItem);
        $this->setPageSize($pageSize);
        $this->setTotalPage(ceil($this->getTotalItem() / $this->getPageSize()));
        if ($page < 1) {
            $this->setPage(1);
        } else if ($page > $this->getTotalPage()) {
            $this->setPage($this->getTotalPage());
        } else {
            $this->setPage($page);
        }

        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (method_exists($this, 'set' . ucfirst($key))) {
                $this->$method($value);
            }
        }
        return true;
    }

    public function style1()
    {
        $page = $this->getPage();
        $totalPage = $this->getTotalPage();
        $baseUrl = $this->getBaseUrl();
        $pageRange = $this->getPageRange();
        $ext = $this->getExt();
        if (!$page || !$totalPage) {
            return array();
        }

        $pageinator = array();
        $pageinator['pageCount'] = $totalPage;
        $pageinator['totalItem'] = $this->getTotalItem();

        // 第一页
        $pageinator['first'] = 1;
        $pageinator['firstUrl'] = $baseUrl . '1' . $ext;

        // 上一页
        $pageinator['previous'] = $page - 1;
        $pageinator['previousUrl'] = $baseUrl . ($page - 1) . $ext;

        // 下一页
        $pageinator['next'] = $page + 1;
        $pageinator['nextUrl'] = $baseUrl . ($page + 1) . $ext;

        //当前页
        $pageinator['PAGE'] = $page;
        $pageinator['current'] = $page;
        $pageinator['currentUrl'] = $baseUrl . $page . $ext;

        // 最后一页
        $pageinator['last'] = $totalPage;
        $pageinator['lastUrl'] = $baseUrl . $totalPage . $ext;

        // 如果page=1, 那么没有 第一页, 上一页,
        if (1 == $page) {
            unset($pageinator['first']);
            unset($pageinator['firstUrl']);
            unset($pageinator['previousUrl']);
            unset($pageinator['previous']);
        }

        // 如果page=总页数, 那么没有 下一页, 最后一页
        if ($page == $totalPage) {
            unset($pageinator['nextUrl']);
            unset($pageinator['next']);
            unset($pageinator['last']);
            unset($pageinator['lastUrl']);
        }

        $pageinator['pages'] = array();
        $count = $pageRange * 2 + 1;

        $pagep = $totalPage - $page;

        $start = max(1, ($pagep < 2) ? ($page - ($count - 1) + $pagep) : $page - 2);
        $end = min($page + ($count - 1), $totalPage);

        for ($i = 1; $i <= $count and $start <= $end; $i++, $start++) {
            $pageinator['pages'][$start] = ($start == $page) ? null : $baseUrl . $start . $ext;
        }

        return $pageinator;
    }

    public function getPage()
    {
        return $this->page;
    }

    public function setPage($page)
    {
        $this->page = $page;
    }

    public function getTotalItem()
    {
        return $this->totalItem;
    }

    public function setTotalItem($totalItem)
    {
        $this->totalItem = $totalItem;
    }

    public function getPageSize()
    {
        return $this->pageSize;
    }

    public function setPageSize($pageSize)
    {
        $this->pageSize = $pageSize;
    }

    public function getTotalPage()
    {
        return $this->totalPage;
    }

    public function setTotalPage($totalPage)
    {
        $this->totalPage = $totalPage;
    }

    public function getBaseUrl()
    {
        return $this->baseUrl;
    }

    public function setBaseUrl($baseUrl)
    {
        $this->baseUrl = $baseUrl;
    }

    public function getPageRange()
    {
        return $this->pageRange;
    }

    public function setPageRange($pageRange)
    {
        $this->pageRange = $pageRange;
    }

    public function getExt()
    {
        return $this->ext;
    }

    public function setExt($ext)
    {
        $this->ext = $ext;
    }

}
